package com.zhongpengcheng.controller.blog;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongpengcheng.blog.dao.pojo.db.BlogDO;
import com.zhongpengcheng.blog.decorator.BlogDecorator;
import com.zhongpengcheng.blog.enums.ResultCodeEnum;
import com.zhongpengcheng.blog.exception.BlogException;
import com.zhongpengcheng.blog.pojo.dto.BlogDTO;
import com.zhongpengcheng.blog.pojo.po.BlogPO;
import com.zhongpengcheng.blog.service.BlogService;
import com.zhongpengcheng.blog.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhongPengCheng
 * @version 1.0
 * @date 2021-11-25 19:55:00
 */
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    /**
     * 新增博客
     */
    @PostMapping("/blog/{id}")
    public Object addBlog(@RequestBody BlogPO po, @PathVariable Long id) {
        BlogDO blogDO = BlogDecorator.of(po).toDO();
        if (!blogService.save(blogDO)) {
            throw new BlogException(ResultCodeEnum.ERROR);
        }
        return BlogDecorator.of(blogDO).toDTO();
    }

    /**
     * 删除博客
     */
    @DeleteMapping("/blog/{id}")
    public BlogDTO deleteBlog(@RequestBody BlogPO po, @PathVariable Long id) {
        BlogDO blogDO = blogService.getById(po.getId());
        if (blogDO == null) {
            throw new BlogException(ResultCodeEnum.ERROR);
        }
        if (!blogService.removeById(po.getId())) {
            throw new BlogException(ResultCodeEnum.ERROR);
        }
        return BlogDecorator.of(blogDO).toDTO();
    }

    /**
     * 更新博客
     */
    @PatchMapping("/blog/{id}")
    public BlogDTO updateBlog(@RequestBody BlogPO po, @PathVariable Long id) {
        AssertUtil.notNull(po.getId(), ResultCodeEnum.REQUEST_REQUIRED_PARAMETER_IS_NULL);
        BlogDO blogDO = BeanUtil.copyProperties(po, BlogDO.class);
        blogService.updateById(blogDO);
        return BlogDecorator.of(blogService.getById(po.getId())).toDTO();
    }

    /**
     * 获取单个博客
     */
    @GetMapping("/blog/{id}")
    public BlogDTO getBlog(@PathVariable Long id) {
        return BlogDecorator.of(blogService.getById(id)).toDTO();
    }

    /**
     * 查询博客列表
     */
    @GetMapping("/blogs")
    public Page<BlogDTO> listBlogs(@RequestBody BlogPO po) {
        Page<BlogDO> result = blogService.page(new Page<>(1, 5));
//        Page<BlogDTO> pageRet = new Page<>(result.getCurrent(), result.getSize(), result.getTotal());

        return new Page<BlogDTO>(result.getCurrent(), result.getSize(), result.getTotal())
                .setRecords(BlogDecorator.of(result.getRecords()).toDTOList());
    }

    /**
     * 查询单个博客
     */
    @GetMapping("/blog")
    public BlogDTO queryBlog(BlogPO po) {
        QueryWrapper<BlogDO> queryWrapper = new QueryWrapper<BlogDO>()
                .eq(po.getTitle() != null, "title", po.getTitle())
                .eq(po.getBlogId() != null, "blog_id", po.getBlogId());

        return BlogDecorator.of(blogService.getOne(queryWrapper)).toDTO();
    }
}
